<?php

namespace App\Http\Controllers\Admins\Feedback;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\Feedback;
use App\Models\OperationalLogs;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class FeedbackController extends BaseAdminController
{
    /**
     * 组织列表
     * @param Request $request
     * @return array|Application|Factory|View
     *
     */
    public function index(Request $request){
        $condition = $request->get('condition','content');#content,username
        if($request->isMethod('post')){
            # 要执行的代码
            $query = Feedback::query()->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $result[] = Feedback::getDataByID($val['id']);
                }
            }
            return ret([
                'total'=>$total,
                'data'=>$result
            ]);
        }
        $admin_id = $request->get('admin_id');
        $subtitle = '意见反馈';
        $compact = compact('subtitle','admin_id');
        return view('v1.admin.feedback.index',$compact);
    }



    /**
     * 删除反馈
     *
     * @param Request $request
     * @param $id
     * @return array
     * @throws \Throwable
     */
    public function delete(Request $request,$id): array
    {
        $where = [
            'id'=>$id,
        ];
        $data = Feedback::query()->where($where);
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['feedback_manage'],
            'operate_item'=>Feedback::query()->where($where)->value('content').'('.$id.')',
            'operate_content'=>OperationalLogs::$operate['feedback_delete'],
        ];
        return AdminDeleteData($data,$logs);
    }

}
